<?php
namespace App\Service\Admin;

class YihuService extends BaseService
{
    //小区医护

    //列表
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbYihu)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $statuss = $this->yihu_statuss;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $typeOne = (array) $this->dbConn($this->dbCate)
                ->where('del', 0)
                ->where('id', $v['type'])
                ->first();
            $type_name = empty($typeOne) ? '-' : $typeOne['title'];
            $pubOne = (array) $this->dbConn($this->dbUser)
                ->where('del', 0)
                ->where('id', $v['uid'])
                ->first();
            $pub_name = empty($pubOne) ? '-' : $pubOne['uname'];
            $dOne = (array) $this->dbConn($this->dbUser)
                ->where('del', 0)
                ->where('id', $v['did'])
                ->first();
            $doctor_name = empty($dOne) ? '-' : $dOne['uname'];
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'type_id' => $v['type'],
                'type_name' => $type_name,
                'uid' => $v['uid'],
                'pub_name' => $pub_name,
                'did' => $v['did'],
                'doctor_name' => $doctor_name,
                'yytime' => $v['yytime'],
                'yydizhi' => $v['yydizhi'],
                'price' => toNumber($v['price']),
                'status' => $v['status'],
                'status_name' => $status_name,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //医护详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbYihu)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $statuss = $this->yihu_statuss;
        $typeOne = (array) $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('id', $res['type'])
            ->first();
        $type_name = empty($typeOne) ? '-' : $typeOne['title'];
        $pubOne = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $res['uid'])
            ->first();
        $pub_name = empty($pubOne) ? '-' : $pubOne['uname'];
        $dOne = (array) $this->dbConn($this->dbUser)
            ->where('del', 0)
            ->where('id', $res['did'])
            ->first();
        $doctor_name = empty($dOne) ? '-' : $dOne['realname'];
        $doctor_name = !$doctor_name ? $dOne['uname'] : '-';
        $status_name = array_key_exists($res['status'], $statuss) ? $statuss[$res['status']] : '-';
        $arys = [
            'id' => $res['id'],
            'type_id' => $res['type'],
            'type_name' => $type_name,
            'uid' => $res['uid'],
            'pub_name' => $pub_name,
            'did' => $res['did'],
            'doctor_name' => $doctor_name,
            'yytime' => $res['yytime'],
            'yydizhi' => $res['yydizhi'],
            'price' => toNumber($res['price']),
            'status' => $res['status'],
            'status_name' => $status_name,
            'addtime' => $res['addtime'],
            'robtime' => $res['robtime'] ? $res['robtime'] : '-',
            'doortime' => $res['doortime'] ? $res['doortime'] : '-',
            'finishtime' => $res['finishtime'] ? $res['finishtime'] : '-',
        ];
        return $this->toback(0, '', $arys);
    }
}